import {
  queryPending,
  saveApplyDetail,
  queryCustomer,
  fetchApplyDetailsShow,
  queryBusiness,
  queryGuarantee,
  queryAbility,
  queryCredit,
  customPhoneShow,
  savePhoneAccess,
  commitCustomer,
  commitBusiness,
  commitCredit,
  commitAbility,
  commitGuarantee,
  auditOpinion,
  saveAudit,
  approvalRecord,
  getChannelList,
  showAllAudit,
  downloadPDF,
  getTaskOperator,
} from '../services/api';
import { message } from 'antd';

export default {
  namespace: 'pendingTask',

  state: {
    pendingList: [],
    page: [],
    loading: false,
    loadingCust: false,
    custListLoading: false,
    saveAuditLoading: false,
    approvalLoading: false,
    custMessgeList: {},
    custTaskList: {},
    idcardVerifyList: {},
    businessList: {},
    creditList: {},
    abilityList: {},
    guaranteeList: {},
    custAuditList: {},
    customList: {},
    guaranteeChannelList: [],
    auditOpinionList: {},
    approvalRecordList: [],
    auditsList: [],
    custTaskListLoading: false,
    applyDetailsState1: false,
    auditStatusList: [false, false, false, false, false],
    taskOperatorList:null
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      // console.log("pendingTask fetch");
      yield put({
        type: 'query',
        payload: {
          loading: true,
        },
      });
      // console.log("pendingTask before call");
      const response = yield call(queryPending, payload);
      // console.log("pendingTask after call");
      if(!response){
        // console.log("fetch got no response");
        return;
      }
      yield put({
        type: 'query',
        payload: {
          pendingList: response.data.list,
          page: response.data.page,
          loading: false,
        },
      });
      // console.log("pendingTask end fetch");
    },

    *fetchApplyProfile({ payload, callback }, { call, put }) {
      yield put({
        type: 'query',
        payload: {
          loadingCust: true,
        },
      });

      const response = yield call(saveApplyDetail, payload);
      // console.log("response")
      // console.log(response)
      yield put({
        type: 'query',
        payload: {
          loadingCust: false,
        },
      });
      yield put({
        type: 'query',
        payload: {
          custMessgeList: response.data.baseInfo,
          custTaskList: response.data.task,
          custAuditList: response.data.audit,
          auditStatusList: [
            response.data.audit&&response.data.audit.status.split("")[0] == '1',
            response.data.audit&&response.data.audit.status.split("")[1] == '1',
            response.data.audit&&response.data.audit.status.split("")[2] == '1',
            response.data.audit&&response.data.audit.status.split("")[3] == '1',
            response.data.audit&&response.data.audit.status.split("")[4] == '1',
          ],
        },
      });
      if (callback) callback();
    },
    *queryCustomer({ payload, callback }, { call, put }) {
      yield put({
        type: 'query',
        payload: {
          custListLoading: true,
        },
      });
      const response = yield call(queryCustomer, payload);
      yield put({
        type: 'query',
        payload: {
          custListLoading: false,
        },
      });
      yield put({
        type: 'query',
        payload: {
          idcardVerifyList: response.data,
        },
      });
      if (callback) callback();
    },
    *queryBusiness({ payload, callback }, { call, put }) {

      const response = yield call(queryBusiness, payload);
      
      yield put({
        type: 'query',
        payload: {
          businessList: response.data,
        },
      });
      if (callback) callback();
    },
    *queryCredit({ payload, callback }, { call, put }) {

      const response = yield call(queryCredit, payload);

      yield put({
        type: 'query',
        payload: {
          creditList: response.data,
        },
      });
      if (callback) callback();
    },
    *queryAbility({ payload, callback }, { call, put }) {

      const response = yield call(queryAbility, payload);

      yield put({
        type: 'query',
        payload: {
          abilityList: response.data,
        },
      });
      if (callback) callback();
    },
    *queryGuarantee({ payload, callback }, { call, put }) {

      const response = yield call(queryGuarantee, payload);

      yield put({
        type: 'query',
        payload: {
          guaranteeList: response.data,
        },
      });
      if (callback) callback();
    },
    *customPhoneShow({ payload, callback }, { call, put }) {
      // console.log("payload",payload)
      const response = yield call(customPhoneShow, payload);
      // console.log("response",response)
      yield put({
        type: 'query',
        payload: {
          customList: response.data,
        },
      });
      if (callback) callback();
    },

    *savePhoneAccess({ payload, callback }, { call, put }) {
      // console.log("payload",payload)
      yield put({
        type: 'savePhoneLoading',
        payload: true
      });
      const response = yield call(savePhoneAccess, payload);
      // console.log("response",response)
      yield put({
        type: 'savePhoneLoading',
        payload: false
      });
      if (response.result.code == '200') {
        message.success("提交成功")
      } else {
        message.error(response.result.message)
      }

      yield put({
        type: 'query',
        payload: {
          data: response.data,
        },
      });
      if (callback) callback()
    },
    *commitCustomer({ payload, callback }, { call, put }) {
      yield put({
        type: 'query',
        payload: {
          custTaskListLoading: true,
        },
      });
      const response = yield call(commitCustomer, payload);
      yield put({
        type: 'query',
        payload: {
          custTaskListLoading: false,
        },
      });
      if (response.result.code == '200') {
        message.success("提交成功")
      } else {
        message.error(response.result.message)
      }

      yield put({
        type: 'query',
        payload: {
          data: response.data,
        },
      });
      if (callback) callback()
    },
    *commitBusiness({ payload, callback }, { call, put }) {
      yield put({
        type: 'query',
        payload: {
          custTaskListLoading: true,
        },
      });
      const response = yield call(commitBusiness, payload);
      yield put({
        type: 'query',
        payload: {
          custTaskListLoading: false,
        },
      });
      if (response.result.code == '200') {
        message.success("提交成功")
      } else {
        message.error(response.result.message)
      }

      yield put({
        type: 'query',
        payload: {
          data: response.data,
        },
      });
      if (callback) callback()
    },
    //资信提交
    *commitCredit({ payload, callback }, { call, put }) {
      yield put({
        type: 'query',
        payload: {
          custTaskListLoading: true,
        },
      });
      const response = yield call(commitCredit, payload);
      yield put({
        type: 'query',
        payload: {
          custTaskListLoading: false,
        },
      });
      if (response.result.code == '200') {
        message.success("提交成功")
      } else {
        message.error(response.result.message)
      }

      yield put({
        type: 'query',
        payload: {
          data: response.data,
        },
      });
      if (callback) callback()
    },
    *commitAbility({ payload, callback }, { call, put }) {
      // console.log("payoad",payload)
      yield put({
        type: 'query',
        payload: {
          custTaskListLoading: true,
        },
      });
      const response = yield call(commitAbility, payload);
      // console.log("response",response)
      yield put({
        type: 'query',
        payload: {
          custTaskListLoading: false,
        },
      });
      if (response.result.code == '200') {
        message.success("提交成功")
      } else {
        message.error(response.result.message)
      }

      yield put({
        type: 'query',
        payload: {
          data: response.data,
        },
      });
      if (callback) callback()
    },
    *commitGuarantee({ payload, callback }, { call, put }) {
      // console.log("payload",payload)
      yield put({
        type: 'query',
        payload: {
          custTaskListLoading: true,
        },
      });
      const response = yield call(commitGuarantee, payload);
      yield put({
        type: 'query',
        payload: {
          custTaskListLoading: false,
        },
      });
      if (response.result.code == '200') {
        message.success("提交成功")
      } else {
        message.error(response.result.message)
      }

      yield put({
        type: 'query',
        payload: {
          data: response.data,
        },
      });
      if (callback) callback()
    },
    *auditOpinion({ payload, callback }, { call, put }) {

      const response = yield call(auditOpinion, payload);

      yield put({
        type: 'query',
        payload: {
          auditOpinionList: response.data,
        },
      });
      if (callback) callback()
    },
    *saveAudit({ payload, callback }, { call, put }) {
      
      yield put({
        type: 'query',
        payload: {
          saveAuditLoading: true,
        },
      });
      const response = yield call(saveAudit, payload);
      yield put({
        type: 'query',
        payload: {
          saveAuditLoading: false,
        },
      });
      if (response.result.code == '200') {
        message.success("提交成功")
      } else {
        message.error(response.result.message)
      }
      yield put({
        type: 'query',
        payload: {
          data: response.data,
        },
      });
      yield put({
        type: 'query',
        payload: {
          result: response.result,
        },
      });
      if (callback) callback()
    },

    *approvalRecord({ payload, callback }, { call, put }) {
      yield put({
        type: 'query',
        payload: {
          approvalLoading: true,
        },
      });
      const response = yield call(approvalRecord, payload);
      // console.log("respons1213131313123", response)
      yield put({
        type: 'query',
        payload: {
          approvalLoading: false,
        },
      });
      if (response.result.code == '200') {
        // message.success("提交成功")
      } else {
        message.error(response.result.message)
      }
      yield put({
        type: 'query',
        payload: {
          approvalRecordList: response.data,
        },
      });
      if (callback) callback()
    },
    //已办任务的所属担保公司
    *getChannelList({ payload, callback }, { call, put }) {
      
      const response = yield call(getChannelList, payload);

      if(!response){
        // console.log("getChannelList got no response");
        return;
      }

      // console.log("respons1213131313123", response)
      yield put({
        type: 'query',
        payload: {
          guaranteeChannelList: response.data,
        },
      });
      if (callback) callback()
    },
    //加载所有的审批意见
    *showAllAudit({ payload, callback }, { call, put }) {
      const response = yield call(showAllAudit, payload);
      yield put({
        type: 'query',
        payload: {
          auditsList: response.data,
        },
      });
      if (callback) callback()
    },
    
    //选择执行人
    *getTaskOperator({ payload, callback }, { call, put }) {
      const response = yield call(getTaskOperator, payload);
      // console.log("response222222", response)
      yield put({
        type: 'query',
        payload: {
          taskOperatorList: response.data,
        },
      });
      if (callback) callback()
    },
     //
     *downloadPDF({ payload, callback }, { call, put }) {
      const response = yield call(downloadPDF, payload);
      // console.log("pendingTask/downloadPDF",response)
      // if(response){
      //   yield put({
      //     type: 'query',
      //     payload: {
      //       data: response.data,
      //     },
      //   });
      // }else{
      //   message.error("服务器返回为空")
      // }
      
      // if (callback) callback()
    },
  },

  reducers: {
    query(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    changeApplyDetailsState1(state, action) {
      return {
        ...state,
        auditStatusList: action.payload,
      };
    },
    savePhoneLoading(state, { payload }) {
      return {
        ...state,
        loading: payload,
      };
    },
  },


};
